<template>
  <view>
    <view class="goods-list">
      <!-- <block v-for="(goods, i) in goodsList" :key="i"> -->
      <view v-for="(goods, i) in goodsList" :key="i" @click="gotoDetail(goods)">
        <my-goods :goods="goods"></my-goods>
        <!-- <view class="goods-item"> -->
          <!-- 左侧盒子 -->
          <!-- <view class="goods-item-left">
            <image :src="goods.goods_small_logo || defaultPic" class="goods-pic"></image>
          </view> -->
          <!-- 右侧盒子 -->
          <!-- <view class="goods-item-right">
            <view class="goods-name">{{goods.goods_name}}</view>
            <view class="goods-info-box">
              <view class="goods-price">¥{{goods.goods_price}}</view>
            </view>
          </view>
        </view> -->
      <!-- </block> -->
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        queryObj: {
          query: '',
          cid: '',
          pagenum: 1,
          pagesize: 10
        },
        // 商品列表数据
        goodsList: [],
        // 总数量,用来实现分页
        total: 0,
        // 默认图片
        // defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
        // 节流阀
        isLoading: false
      };
    },
    onLoad(options) {
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      // console.log(this.queryObj);
      this.getGoodsList()
    },
    methods: {
      async getGoodsList(cb) {
        // 打开节流阀
        this.isLoading = true
        const {data: res} = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
        // 关闭节流阀
        this.isLoading = false
        cb && cb()
        // console.log(res);
        if(res.meta.status !== 200) {
          return uni.$showMsg()
        }
        // this.goodsList = res.message.goods
        this.goodsList = [...this.goodsList, ...res.message.goods]
        this.total = res.message.total
      },
      gotoDetail(goods) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + goods.goods_id
        })
      }
    },
    onReachBottom() {
      // 判断是否还有下一页数据
      if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('数据加载完毕！')
      if(this.isLoading) return
      this.queryObj.pagenum++
      this.getGoodsList()
    },
    // 下拉刷新的事件
    onPullDownRefresh() {
      // 重置关键数据
      this.queryObj.pagenum = 1,
      this.total = 0,
      this.isLoading = false,
      this.goodsList = []
      // 重新发起请求,停止下拉刷新
      this.getGoodsList(() => uni.stopPullDownRefresh())
    }
  }
</script>

<style lang="scss">
  // .goods-item {
  //   display: flex;
  //   padding: 10px 5px;
  //   border-bottom: 1px solid #f0f0f0;
  //   .goods-item-left {
  //     margin-right: 5px;
  //     .goods-pic {
  //       width: 100px;
  //       height: 100px;
  //       display: block;
  //     }
  //   }
  //   .goods-item-right {
  //     display: flex;
  //     flex-direction: column;
  //     justify-content: space-between;
  //     .goods-name {
  //       font-size: 13px;
  //     }
  //     .goods-info-box {
  //       color: #c00000;
  //       font-size: 16px;
  //     }
  //   }
  // }
</style>
